/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.anny.stream.operatin.web;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import com.jeeplus.modules.wxutil.WeiXinUserInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.modules.anny.stream.operatin.entity.AnnyOperatin;
import com.jeeplus.modules.anny.stream.operatin.service.AnnyOperatinService;

/**
 * 积分流水管理Controller
 * @author clf
 * @version 2018-10-18
 */
@Controller
@RequestMapping(value = "${adminPath}/anny/stream/operatin/annyOperatin")
public class AnnyOperatinController extends BaseController {

	@Autowired
	private AnnyOperatinService annyOperatinService;

	@ModelAttribute
	public AnnyOperatin get(@RequestParam(required=false) String id) {
		AnnyOperatin entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = annyOperatinService.get(id);
		}
		if (entity == null){
			entity = new AnnyOperatin();
		}
		return entity;
	}

	/**
	 * 用户查看自己的积分明细  分页
	 * @param request
	 */
	@ResponseBody
	@RequestMapping("userOperatin")
	public  Map<String, Object> getUserOperatin(HttpServletRequest request,HttpServletResponse response){
//		String id="1";
		String id= WeiXinUserInfo.getWxUser(request).getId();//获取微信用户？？？
		AnnyOperatin annyOperatin=new AnnyOperatin();
		annyOperatin.setAccountId(id);
		annyOperatin.setPage(new Page<AnnyOperatin>(request,response));
		Page<AnnyOperatin> page = annyOperatinService.getOperatinByUser(annyOperatin);
		return getBootstrapData(page);
	}
	/**
	 * 积分流水信息列表页面
	 */
	@RequiresPermissions("anny:stream:operatin:annyOperatin:list")
	@RequestMapping(value = {"list", ""})
	public String list() {
		return "modules/anny/stream/operatin/annyOperatinList";
	}

	/**
	 * 积分流水信息列表数据
	 */
	@ResponseBody
	@RequiresPermissions("anny:stream:operatin:annyOperatin:list")
	@RequestMapping(value = "data")
	public Map<String, Object> data(AnnyOperatin annyOperatin, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<AnnyOperatin> page = annyOperatinService.findPage(new Page<AnnyOperatin>(request, response), annyOperatin);
		return getBootstrapData(page);
	}

	/**
	 * 查看，增加，编辑积分流水信息表单页面
	 */
	@RequiresPermissions(value={"anny:stream:operatin:annyOperatin:view","anny:stream:operatin:annyOperatin:add","anny:stream:operatin:annyOperatin:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(AnnyOperatin annyOperatin, Model model) {
		model.addAttribute("annyOperatin", annyOperatin);
		if(StringUtils.isBlank(annyOperatin.getId())){//如果ID是空为添加
			model.addAttribute("isAdd", true);
		}
		return "modules/anny/stream/operatin/annyOperatinForm";
	}

	/**
	 * 保存积分流水信息
	 */
	@RequiresPermissions(value={"anny:stream:operatin:annyOperatin:add","anny:stream:operatin:annyOperatin:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(AnnyOperatin annyOperatin, Model model, RedirectAttributes redirectAttributes) throws Exception{
		if (!beanValidator(model, annyOperatin)){
			return form(annyOperatin, model);
		}
		//新增或编辑表单保存
		annyOperatinService.save(annyOperatin);//保存
		addMessage(redirectAttributes, "保存积分流水信息成功");
		return "redirect:"+Global.getAdminPath()+"/anny/stream/operatin/annyOperatin/?repage";
	}

	/**
	 * 删除积分流水信息
	 */
	@ResponseBody
	@RequiresPermissions("anny:stream:operatin:annyOperatin:del")
	@RequestMapping(value = "delete")
	public AjaxJson delete(AnnyOperatin annyOperatin, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		annyOperatinService.delete(annyOperatin);
		j.setMsg("删除积分流水信息成功");
		return j;
	}

	/**
	 * 批量删除积分流水信息
	 */
	@ResponseBody
	@RequiresPermissions("anny:stream:operatin:annyOperatin:del")
	@RequestMapping(value = "deleteAll")
	public AjaxJson deleteAll(String ids, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		String idArray[] =ids.split(",");
		for(String id : idArray){
			annyOperatinService.delete(annyOperatinService.get(id));
		}
		j.setMsg("删除积分流水信息成功");
		return j;
	}

//	/**
//	 * 导出excel文件
//	 */
//	@ResponseBody
//	@RequiresPermissions("anny:stream:operatin:annyOperatin:export")
//    @RequestMapping(value = "export", method=RequestMethod.POST)
//    public AjaxJson exportFile(AnnyOperatin annyOperatin, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
//		AjaxJson j = new AjaxJson();
//		try {
//            String fileName = "积分流水信息"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
//            Page<AnnyOperatin> page = annyOperatinService.findPage(new Page<AnnyOperatin>(request, response, -1), annyOperatin);
//    		new ExportExcel("积分流水信息", AnnyOperatin.class).setDataList(page.getList()).write(response, fileName).dispose();
//    		j.setSuccess(true);
//    		j.setMsg("导出成功！");
//    		return j;
//		} catch (Exception e) {
//			j.setSuccess(false);
//			j.setMsg("导出积分流水信息记录失败！失败信息："+e.getMessage());
//		}
//			return j;
//    }
//
//	/**
//	 * 导入Excel数据
//
//	 */
//	@RequiresPermissions("anny:stream:operatin:annyOperatin:import")
//    @RequestMapping(value = "import", method=RequestMethod.POST)
//    public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
//		try {
//			int successNum = 0;
//			int failureNum = 0;
//			StringBuilder failureMsg = new StringBuilder();
//			ImportExcel ei = new ImportExcel(file, 1, 0);
//			List<AnnyOperatin> list = ei.getDataList(AnnyOperatin.class);
//			for (AnnyOperatin annyOperatin : list){
//				try{
//					annyOperatinService.save(annyOperatin);
//					successNum++;
//				}catch(ConstraintViolationException ex){
//					failureNum++;
//				}catch (Exception ex) {
//					failureNum++;
//				}
//			}
//			if (failureNum>0){
//				failureMsg.insert(0, "，失败 "+failureNum+" 条积分流水信息记录。");
//			}
//			addMessage(redirectAttributes, "已成功导入 "+successNum+" 条积分流水信息记录"+failureMsg);
//		} catch (Exception e) {
//			addMessage(redirectAttributes, "导入积分流水信息失败！失败信息："+e.getMessage());
//		}
//		return "redirect:"+Global.getAdminPath()+"/anny/stream/operatin/annyOperatin/?repage";
//    }
//
//	/**
//	 * 下载导入积分流水信息数据模板
//	 */
//	@RequiresPermissions("anny:stream:operatin:annyOperatin:import")
//    @RequestMapping(value = "import/template")
//    public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
//		try {
//            String fileName = "积分流水信息数据导入模板.xlsx";
//    		List<AnnyOperatin> list = Lists.newArrayList();
//    		new ExportExcel("积分流水信息数据", AnnyOperatin.class, 1).setDataList(list).write(response, fileName).dispose();
//    		return null;
//		} catch (Exception e) {
//			addMessage(redirectAttributes, "导入模板下载失败！失败信息："+e.getMessage());
//		}
//		return "redirect:"+Global.getAdminPath()+"/anny/stream/operatin/annyOperatin/?repage";
//    }

}